package org.mybatis.generator.plugins;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.List;

/**
 * Created by Sean on 14-2-25.
 */
public class PageInfoPlugin extends PluginAdapter {

    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public boolean sqlMapDocumentGenerated(Document document,
                                           IntrospectedTable introspectedTable) {
        StringBuffer sb = new StringBuffer();
        List<IntrospectedColumn> columns = introspectedTable.getBaseColumns();
        for (IntrospectedColumn column : columns){
            sb.append("                 <if test=\"entity.").append(column.getJavaProperty()).append("!= null\" >").append("\n")
                    .append("                      ").append(column.getActualColumnName()).append(" = #{entity.").append(column.getJavaProperty()).append(",jdbcType=").append(column.getJdbcTypeName()).append("} and ").append("\n")
                    .append("                 </if>").append("\n");
        }
        XmlElement sql = new XmlElement("sql");
        sql.getAttributes().add(new Attribute("id", "selectConditionBean"));
        sql.getElements().add(new TextElement("<where>\n" +
                "            <trim suffixOverrides=\"and\">\n" +
                "                <if test=\"entity != null\">\n" +
                                    sb.toString() +
                "                </if>\n" +
                "            </trim>\n" +
                "            <trim suffixOverrides=\"and\">\n" +
                "                <if test=\"whereSql!=null\">\n" +
                "                    and ${whereSql}\n" +
                "                </if>\n" +
                "            </trim>\n" +
                "        </where>"));


        XmlElement selectPageInfo = new XmlElement("select");
        selectPageInfo.getAttributes().add(new Attribute("id", "getListByEntityAndPageInfo"));
        selectPageInfo.getAttributes().add(new Attribute("resultMap", "BaseResultMap"));
        selectPageInfo.getElements().add(new TextElement("select\n" +
                "        <include refid=\"Base_Column_List\" />\n" +
                "        from " + introspectedTable.getTableConfiguration().getTableName() + "\n" +
                "        <include refid=\"selectConditionBean\" />\n" +
                "        <if test=\"orderBy!=null\">\n" +
                "            ${orderBy}\n" +
                "        </if>\n" +
                "        <if test=\"pageInfo!=null\">\n" +
                "            limit ${pageInfo.startIndex} ,${pageInfo.pagesize}\n" +
                "        </if>"));

        XmlElement count = new XmlElement("select");
        count.getAttributes().add(new Attribute("id", "getTotalByBean"));
        count.getAttributes().add(new Attribute("resultType", "java.lang.Integer"));
        count.getElements().add(new TextElement("select count(*) from " + introspectedTable.getTableConfiguration().getTableName() + "\n" +
                "        <include refid=\"selectConditionBean\"/>"));

        document.getRootElement().getElements().add(sql);
        document.getRootElement().getElements().add(selectPageInfo);
        document.getRootElement().getElements().add(count);
        return true;
    }

    @Override
    public boolean clientGenerated(Interface interfaze,
                                   TopLevelClass topLevelClass,
                                   IntrospectedTable introspectedTable) {
        return true;
    }

}
